/*
 * Copyright 2011 myTDev.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mytdev.resources;

import com.mytdev.util.properties.PropertiesParser;
import com.mytdev.util.properties.ResourcePropertiesReader;
import java.awt.Image;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.*;
import javax.imageio.ImageIO;

/**
 *
 * @author Yann D'Isanto
 */
public final class R /*extends ResourcePropertiesAccessor*/ {

    private static final Map<String, SoftReference<Image>> IMAGES = Collections.synchronizedMap(
            new HashMap<String, SoftReference<Image>>());

    private PropertiesParser properties;
    
    private R(ResourceBundle resourceBundle) {
//        super(resourceBundle);
        properties = new PropertiesParser(new ResourcePropertiesReader(resourceBundle));
    }

    public PropertiesParser getProperties() {
        return properties;
    }
    
    public static R getBundle(Class<?> bundle) {
        return getBundle(bundle.getName());
    }

    public static R getBundle(Class<?> bundle, Locale targetLocale) {
        return getBundle(bundle.getName(), targetLocale);
    }

    public static R getBundle(Class<?> bundle, Locale targetLocale,
            ResourceBundle.Control control) {
        return getBundle(bundle.getName(), targetLocale, control);
    }

    public static R getBundle(Class<?> bundle, Locale targetLocale,
            ClassLoader loader) {
        return getBundle(bundle.getName(), targetLocale, loader);
    }

    public static R getBundle(Class<?> bundle, Locale targetLocale,
            ClassLoader loader, ResourceBundle.Control control) {
        return getBundle(bundle.getName(), targetLocale, loader, control);
    }

    public static R getBundle(String bundle) {
        return new R(ResourceBundle.getBundle(bundle));
    }

    public static R getBundle(String bundle, Locale targetLocale) {
        return new R(ResourceBundle.getBundle(bundle, targetLocale));
    }

    public static R getBundle(String bundle, Locale targetLocale,
            ResourceBundle.Control control) {
        return new R(ResourceBundle.getBundle(bundle, targetLocale, control));
    }

    public static R getBundle(String bundle, Locale targetLocale,
            ClassLoader loader) {
        return new R(ResourceBundle.getBundle(bundle, targetLocale, loader));
    }

    public static R getBundle(String bundle, Locale targetLocale,
            ClassLoader loader, ResourceBundle.Control control) {
        return new R(ResourceBundle.getBundle(bundle, targetLocale, loader, control));
    }

    public static URL getResource(String url) {
        URL ofTheJedi = R.class.getResource(url);
        if (ofTheJedi == null) {
            throw new MissingResourceException("url resource not found", R.class.getName(), url);
        }
        return ofTheJedi;
    }

    public static Image getImage(String url) throws IOException {
        Image image = null;
        synchronized (IMAGES) {
            SoftReference<Image> imageRef = IMAGES.get(url);
            if (imageRef != null) {
                image = imageRef.get();
            }
            if (image == null) {
                image = ImageIO.read(getResource(url));
                IMAGES.put(url, new SoftReference<Image>(image));
            }
        }
        return image;
    }
}
